﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TreeContainer;
using Arbore;

namespace PresentationWpf
{
   public class LoaderTree
    {
         List<string> listaEXI ;
         TreeContainer.TreeContainer Tree;
         public LoaderTree()
         {
             listaEXI = new List<string>();
         }
       public  void loadeTree(Arbore.Arbore ab,ref TreeNode O, ref TreeContainer.TreeContainer T)
         {

             Tree = T;
             List<string> listaEXI=new List<string>();
           var i = ab.Radacina;
            O.Content = i.Value;
            listaEXI = new List<string>();
            loaderArbore(ab.Radacina,O);

        }


        private void loaderArbore(Nod i, TreeContainer.TreeNode t)
        {
           

            foreach (Nod item in i.Childrens)
            {
                if (item != null)
                {
                    TreeContainer.TreeNode treeItem = new TreeContainer.TreeNode();
                    treeItem.Content = item.Value;
                    var nume = RandomString(20);
                    while(listaEXI.Contains(nume))
                    {
                        nume=RandomString(20);
                    }
                    treeItem.Name = nume;
                    listaEXI.Add(nume);
                   // treeItem.TreeParent = t.Name;
                    Tree.AddNode(item.Value, treeItem.Name,t.Name);
                    loaderArbore(item, treeItem);

                }

            }
        }

        private string RandomString(int size)
        {
            StringBuilder builder = new StringBuilder();
            Random random = new Random();
            char ch;
            for (int i = 0; i < size; i++)
            {
                ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
                builder.Append(ch);
            }

            return builder.ToString();
        }

    }
}
